package org.swidgets.base.smessagebox;

import org.swidgets.base.spanels.SFrame;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

public class DragableFrame extends SFrame {

	  private boolean dragging;
	  private int dragStartX, dragStartY;
	 
	
	public DragableFrame(){
		super();
		labelCaption.setStyleName("DialogTraspHeader");
		labelCaption.setStyle(labelCaption.TITLE_LABEL_STYLE);
		labelCaption.addMouseListener(new MouseListener(){

			public void onMouseDown(Widget sender, int x, int y) {
				dragging = true;
			    DOM.setCapture(DragableFrame.this.labelCaption.getElement());
			    dragStartX = x;
			    dragStartY = y;
			  }

			  public void onMouseEnter(Widget sender) {
			  }

			  public void onMouseLeave(Widget sender) {
			  }

			  public void onMouseMove(Widget sender, int x, int y) {
			    if (dragging) {
			      int absX = x + getAbsoluteLeft();
			      int absY = y + getAbsoluteTop();
			      setPopupPosition(absX - dragStartX, absY - dragStartY);
			    }
			  }

			  public void onMouseUp(Widget sender, int x, int y) {
			    dragging = false;
			    DOM.releaseCapture(DragableFrame.this.labelCaption.getElement());
			  }
			  
			  
				public void setPopupPosition(int left, int top) {
				    if (left < 0) {
				      left = 0;
				    }
				    if (top < 0) {
				      top = 0;
				    }

				    Element elem = DragableFrame.this.getParent().getElement();
				    DOM.setStyleAttribute(elem, "left", left + "px");
				    DOM.setStyleAttribute(elem, "top", top + "px");
				  }
			  
			  
			
		});
		
		
		
	}
	
	public void setCaption(String caption){
		this.labelCaption.setText(caption);
	}


}
